//
//  OrderDetailsViewController.m
//  HongFuShop
//
//  Created by chirui on 2017/8/15.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "OrderDetailsViewController.h"
#import "MyOrderTableViewCell.h"

@interface OrderDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>{

    UITableView * _tableView;
}

@end

@implementation OrderDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    TitleView(@"订单详情")
    
    NSLog(@"dataDic==%@",self.dataDic);
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_public_return" target:self action:@selector(back)];
    
    [self creatTableView];
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:[MyOrderTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[MyOrderTableViewCell reuseIdentifier]];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSArray * array = self.dataDic[@"order_goods"];
    
    return array.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 120;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return 300;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = self.dataDic[@"order_goods"][indexPath.row];
    MyOrderTableViewCell  * cell = [tableView dequeueReusableCellWithIdentifier:[MyOrderTableViewCell reuseIdentifier] forIndexPath:indexPath];
    
    cell.goodsName.frame = CGRectMake(117, 11, WIDTH - 127, 20);
    cell.goodsName.text =  [NSString stringWithFormat:@"%@", dic[@"goods_name"]];
    cell.goodsName.numberOfLines = 2;
    
    [cell.goodsImge sd_setImageWithURL:ImageUrl(dic[@"src"]) placeholderImage:ImageName(@"img_new_default")];
    cell.goodsNumber.text = [NSString stringWithFormat:@"x%@",dic[@"count"]];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSLog(@"dic == %@",dic);
    
    return cell;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView * heardView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 300)];
    heardView.backgroundColor =[UIColor groupTableViewBackgroundColor];

    UIView * statisView =[[UIView alloc]initWithFrame:CGRectMake((WIDTH-100)/2, 30, 100, 40)];
    statisView.backgroundColor = [UIColor clearColor];
    
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 5, 30, 30)];
    
    [statisView addSubview:imageView];
    
    if ([self.dataDic[@"order_status"] isEqualToString:@"create"]) {
        imageView.image = ImageName(@"icon_oder_delivery");
         [self creatLabelFrame:CGRectMake(35, 5, 76, 30) andTextColor:[UIColor redColor] andText:@"待发货" andFont:16 andAliment:NSTextAlignmentLeft onView:statisView];
    }
    
    if ([self.dataDic[@"order_status"] isEqualToString:@"deliver"]) {
        imageView.image = ImageName(@"icon_oder_gr");
        [self creatLabelFrame:CGRectMake(35, 5, 76, 30) andTextColor:[UIColor redColor] andText:@"待收货" andFont:16 andAliment:NSTextAlignmentLeft onView:statisView];
    }

    if ([self.dataDic[@"order_status"] isEqualToString:@"done"]) {
        imageView.image = ImageName(@"icon_oder_ok");
        [self creatLabelFrame:CGRectMake(35, 5, 76, 30) andTextColor:[UIColor redColor] andText:@"已完成" andFont:16 andAliment:NSTextAlignmentLeft onView:statisView];
    }

    UIView * addressView =[[UIView alloc]initWithFrame:CGRectMake(0, 100, WIDTH, 100)];
    addressView.backgroundColor =[UIColor whiteColor];
    
    
    UIImageView * addressImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 75/2, 25, 25)];
    addressImageView.image = ImageName(@"icon_oder_address");
    [addressView addSubview:addressImageView];
    
     [self creatLabelFrame:CGRectMake(35, 20, 120, 20) andTextColor:[UIColor blackColor] andText:self.dataDic[@"name"] andFont:15 andAliment:NSTextAlignmentLeft onView:addressView];
    
     [self creatLabelFrame:CGRectMake(160, 20, WIDTH - 170, 20) andTextColor:[UIColor blackColor] andText:self.dataDic[@"phone"] andFont:15 andAliment:NSTextAlignmentRight onView:addressView];
    
    [self creatLabelFrame:CGRectMake(35, 60, WIDTH - 40, 20) andTextColor:[UIColor blackColor] andText:self.dataDic[@"addr"] andFont:15 andAliment:NSTextAlignmentLeft onView:addressView];
    
    [heardView addSubview:addressView];
    
    UIView * orderView =[[UIView alloc]initWithFrame:CGRectMake(0, 210, WIDTH, 80)];
    orderView.backgroundColor =[UIColor whiteColor];
    
    [self creatLabelFrame:CGRectMake(10, 15, 120, 20) andTextColor:RGB(0x4c4c4c) andText:@"订单编号" andFont:15 andAliment:NSTextAlignmentLeft onView:orderView];
    
    [self creatLabelFrame:CGRectMake(140, 15, WIDTH - 150, 20) andTextColor:RGB(0x4c4c4c) andText:self.dataDic[@"order_sn"] andFont:15 andAliment:NSTextAlignmentRight onView:orderView];
    
    [self creatLabelFrame:CGRectMake(10, 45, 120, 20) andTextColor:RGB(0x4c4c4c) andText:self.dataDic[@"order_time_dict"] andFont:15 andAliment:NSTextAlignmentLeft onView:orderView];
    
    [self creatLabelFrame:CGRectMake(140, 45, WIDTH - 150, 20) andTextColor:RGB(0x4c4c4c) andText:self.dataDic[@"order_time"]andFont:15 andAliment:NSTextAlignmentRight onView:orderView];
    
    
    [heardView addSubview:orderView];
    
    [heardView addSubview:statisView];
    

    return heardView;
}

-(void)creatLabelFrame:(CGRect)rec andTextColor:(UIColor*)col andText:(NSString *)text andFont:(CGFloat)flo andAliment:(NSTextAlignment)aliment onView:(UIView *)view {


    UILabel * statisLabel =[[UILabel alloc]initWithFrame:rec];
    statisLabel.font = [UIFont systemFontOfSize:flo];
    statisLabel.textColor = col;
    statisLabel.textAlignment = aliment;
    statisLabel.text = text;
    [view addSubview:statisLabel];


}

@end
